package com.ohc.leetcode.链表;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 欧皓辰
 * @Date 2021/12/23 23:30
 * @Version 1.0
 * https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
 */
public class 尾到头打印链表 {

    private int[] arr = new int[10];

    public static void main(String[] args) {
        尾到头打印链表 obj = new 尾到头打印链表();
//        obj.method02()
    }

    public static int[] method01(ListNode head) {
        List<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        int[] arr = new int[list.size()];
        for (int i = 0; i < list.size(); ++i) {
            arr[i] = list.get(list.size() - i);
        }
        return arr;
    }

    /**
     * 递归方法
     *
     * @param head
     * @return
     */
    public int[] method02(ListNode head) {
        ArrayList<Integer> list = new ArrayList<>();
        method02DiGui(head, list);
        return this.arr;
    }

    public void method02DiGui(ListNode node, List<Integer> list) {
        if (node == null) return;
        method02DiGui(node.next,list);
        list.add(node.val);
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
